package cn.iocoder.devlop.module.annual.report.alterstockinfo.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.devlop.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.devlop.module.annual.report.alterstockinfo.controller.vo.AlterstockinfoCreateReqVO;
import cn.iocoder.devlop.module.annual.report.alterstockinfo.controller.vo.AlterstockinfoOverwriteVO;
import cn.iocoder.devlop.module.annual.report.alterstockinfo.convert.AlterstockinfoConvert;
import cn.iocoder.devlop.module.annual.report.alterstockinfo.dal.AlterstockinfoMapper;
import cn.iocoder.devlop.module.annual.report.alterstockinfo.dal.dataobject.AlterstockinfoDO;
import cn.iocoder.devlop.module.annual.report.baseinfo.dal.dataobject.BaseinfoDO;
import cn.iocoder.devlop.module.annual.report.baseinfo.service.BaseinfoService;
import cn.iocoder.devlop.module.annual.report.common.service.EnttypeEntclassifyService;
import cn.iocoder.devlop.module.annual.report.marpripinfo.dal.dataobject.MarpripinfoDO;
import cn.iocoder.devlop.module.annual.report.marpripinfo.service.MarpripinfoService;
import cn.iocoder.devlop.module.annual.report.utils.IdGenerateUtil;
import cn.iocoder.devlop.module.annual.report.utils.ObjectComparisonUtil;
import cn.iocoder.devlop.module.annual.report.utils.ReportYearHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 年报股权变更信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class AlterstockinfoServiceImpl implements AlterstockinfoService {

    @Resource
    private AlterstockinfoMapper alterstockinfoMapper;

    @Resource
    private BaseinfoService baseinfoService;

    @Resource
    private MarpripinfoService marpripinfoService;

    @Resource
    private EnttypeEntclassifyService enttypeEntclassifyService;


    @Override
    public List<AlterstockinfoDO> getAlterstockinfoList(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        return alterstockinfoMapper.selectListByAncheid(ancheid);
    }

    @Override
    public void deleteAlterstockinfoAll(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        LambdaQueryWrapperX<AlterstockinfoDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(AlterstockinfoDO::getAncheid,ancheid);
        alterstockinfoMapper.delete(wrapperX);
    }

    @Override
    public void createAlterstockinfoByList(String regno, String ancheyear, List<AlterstockinfoCreateReqVO> alterstockinfoList) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        MarpripinfoDO marpripinfo = marpripinfoService.getMarpripinfo(regno);
        String entclassify = enttypeEntclassifyService.getEntclassify(marpripinfo.getEnttype());
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(regno, ancheyear);
        if (null == baseinfo){
            baseinfoService.initBaseinfo(regno,ancheyear);
        }
        BaseinfoDO baseinfoDO = new BaseinfoDO();
        baseinfoDO.setAncheid(IdGenerateUtil.getAncheid(regno, ancheyear));
        baseinfoDO.setStocktransign("2");
        if (CollUtil.isNotEmpty(alterstockinfoList)) {
            List<AlterstockinfoDO> alterstockinfoDOS = AlterstockinfoConvert.INSTANCE.convertList01(alterstockinfoList);
            for (AlterstockinfoDO alterstockinfo : alterstockinfoDOS) {
                alterstockinfo.setEntclassify(entclassify);
                alterstockinfo.setAncheid(ancheid);
                alterstockinfo.setInvaltid(IdUtil.simpleUUID());
            }
            baseinfoDO.setStocktransign("1");
            alterstockinfoMapper.insertBatch(alterstockinfoDOS);
        }
        baseinfoService.baseinfoUpdate(baseinfoDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void overwriteAlterstockinfo(AlterstockinfoOverwriteVO updateReqVO) {
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        if (ObjUtil.isNotEmpty(baseinfo)){
            if (StrUtil.equals(baseinfo.getState(),"02") ||StrUtil.equals(baseinfo.getState(),"03")) {
                List<AlterstockinfoDO> oldAlterstockinfoList = getAlterstockinfoList(updateReqVO.getRegno(), updateReqVO.getAncheyear());
                List<AlterstockinfoCreateReqVO> newAlterstockinfoList = updateReqVO.getAlterstockinfoList();
                List<AlterstockinfoCreateReqVO> oldreqs = AlterstockinfoConvert.INSTANCE.convertList3(oldAlterstockinfoList);
                ObjectComparisonUtil.compareObjectList(oldreqs,newAlterstockinfoList,IdGenerateUtil.getAncheid(updateReqVO.getRegno(), updateReqVO.getAncheyear()),"股权变更");
            }
        }
        deleteAlterstockinfoAll(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        createAlterstockinfoByList(updateReqVO.getRegno(), updateReqVO.getAncheyear(), updateReqVO.getAlterstockinfoList());
        baseinfoService.updateState(updateReqVO.getRegno(), updateReqVO.getAncheyear());
    }

}
